package com.world.tg.shiro;

import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Locale;

public class MyLogoutFilter extends LogoutFilter {
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        Subject subject = this.getSubject(request, response);
        if (this.isPostOnlyLogout() && !WebUtils.toHttp(request).getMethod().toUpperCase(Locale.ENGLISH).equals("POST")) {
            return this.onLogoutRequestNotAPost(request, response);
        } else {
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            httpServletResponse.setStatus(200);
            httpServletResponse.setContentType("application/json;charset=utf-8");
            PrintWriter out = httpServletResponse.getWriter();
            String json = "";
            try {
                subject.logout();
                json = "{\"code\":\"200\",\"msg\":\"退出成功\"}";
                out.println(json);
                out.flush();
                out.close();
                return true;
            } catch (SessionException var6) {
                json = "{\"code\":\"0\",\"msg\":\"退出失败\"}";
                out.println(json);
                out.flush();
                out.close();
            }finally {
                if(out != null){
                    out.close();
                }
            }
            return false;
        }
    }
}
